//
//  WBOrginalView.swift
//  WeiBo
//
//  Created by chenguanglei on 16/11/29.
//  Copyright © 2016年 itheima. All rights reserved.
//

import UIKit
import SDWebImage

let WBOrginalViewMargin: CGFloat = 10

class WBOrginalView: UIView {

    var bottom: Constraint?
    
    var orginalViewModel: WBHomeViewModel?{
        
        didSet{
            
            // 设置头像
            iconImageView.sd_setImage(with: orginalViewModel?.profile_image_url)
            
            // 昵称
            nameLabel.text = orginalViewModel?.status?.user?.screen_name
            
            // 等级
            levelImageView.image = orginalViewModel?.mbrank
            
            // 认证图片
            veriftyImageView.image = orginalViewModel?.verified
            
            contentLabel.text = orginalViewModel?.status?.text
            
            souceLabel.text = orginalViewModel?.source_string
            
            timeLabel.text = orginalViewModel?.time_String
            
//            pictureView.pic_urls = orginalViewModel?.status?.pic_urls
            
            // 解包判断
            guard let pic_urls = orginalViewModel?.status?.pic_urls else {
                return
            }
            
            bottom?.uninstall()
            
            if pic_urls.count > 0  {
                
                // 有配图
                pictureView.isHidden = false
                
                
                // 让底部的 self.bottom.equalTo(pictureView.snp_bottom)
                self.snp_updateConstraints(closure: { (make) in
                    bottom = make.bottom.equalTo(pictureView.snp_bottom).constraint
                })
                
                // 传递数据
                pictureView.pic_urls = pic_urls
                
                // 改变宽和高
                
            }else {
                
                // 没有配图
                pictureView.isHidden = true
                 //self.bottom.equalTo(contentLabel.snp_bottom)
                self.snp_updateConstraints(closure: { (make) in
                    bottom =  make.bottom.equalTo(contentLabel.snp_bottom).constraint
                })
            }
            
        }
        
    }
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        
        super.init(coder: aDecoder)
        
    }

    
    private func setupUI(){
    
        backgroundColor = UIColor.white
        
        addSubview(iconImageView)
        addSubview(veriftyImageView)
        addSubview(nameLabel)
        addSubview(levelImageView)
        addSubview(timeLabel)
        addSubview(souceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)
        // 设置约束 -- 固定死一个控件很重要,其他的都按照这个布局就可以了!!!
        
        // 头像 左 10 上 10 宽高 35
        iconImageView.snp_makeConstraints { (make) in
            
            make.left.equalTo(self.snp_left).offset(WBOrginalViewMargin)
            make.top.equalTo(self.snp_top).offset(WBOrginalViewMargin)
            
            make.width.equalTo(35)
            make.height.equalTo(35)
        }
        
        // 认证图标的约束 centerX = 头像的右边; centerY = 头像的底部
        veriftyImageView.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(iconImageView.snp_right)
            make.centerY.equalTo(iconImageView.snp_bottom)
            
        }
        
        // 认证等级 左边 和 上边
        nameLabel.snp_makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp_right).offset(WBOrginalViewMargin)
            make.top.equalTo(iconImageView.snp_top)
        }
        
        
        // 认证等级
        levelImageView.snp_makeConstraints { (make) in
            make.centerY.equalTo(nameLabel.snp_centerY)
            make.left.equalTo(nameLabel.snp_right).offset(WBOrginalViewMargin)
        }
        
        //
        timeLabel.snp_makeConstraints { (make) in
            make.left.equalTo(iconImageView.snp_right).offset(WBOrginalViewMargin)
            
            make.top.equalTo(nameLabel.snp_bottom).offset(WBOrginalViewMargin * 0.5)
        }
        
        souceLabel.snp_makeConstraints { (make) in
            make.centerY.equalTo(timeLabel.snp_centerY)
            
            make.left.equalTo(timeLabel.snp_right).offset(WBOrginalViewMargin)
        }
        
        // 微博内容的约束
        contentLabel.snp_makeConstraints { (make) in
            
            make.left.equalTo(self.snp_left).offset(WBOrginalViewMargin)
            make.top.equalTo(iconImageView.snp_bottom).offset(WBOrginalViewMargin)
            make.right.equalTo(self.snp_right).offset(-WBOrginalViewMargin)
            
        }
        
        
        pictureView.snp_makeConstraints { (make) in
            make.left.equalTo(self.snp_left).offset(WBOrginalViewMargin)
            make.top.equalTo(contentLabel.snp_bottom)
            
            
        }
        
        // 自动计算行高的时候,一定要告诉最后一个控件底部在哪里!!!
        
        // 告诉self 底部在哪里???
        self.snp_makeConstraints { (make) in
            bottom = make.bottom.equalTo(pictureView.snp_bottom).constraint
        }
     
    }
    
    
    /// 头像
    private lazy var iconImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    /// 认证图片
    private lazy var veriftyImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
    
    /// 昵称
    private lazy var nameLabel: UILabel = UILabel(labelText: "昵称", labelFont: 14, textColor: UIColor.darkGray, align: NSTextAlignment.left)
    
    /// 等级图片
    private lazy var levelImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership_level6"))
    
    /// 发布时间
    private lazy var timeLabel: UILabel = UILabel(labelText: "时间", labelFont: 12, textColor: UIColor.orange, align: NSTextAlignment.left)
    
    /// 来源
    private lazy var souceLabel: UILabel = UILabel(labelText: "来源", labelFont: 12, textColor: UIColor.gray, align: NSTextAlignment.left)
    /// 内容
    
    private lazy var contentLabel: UILabel = UILabel(labelText: "微博内容", labelFont: 16, textColor: UIColor.black, align: NSTextAlignment.left)
    
    private lazy var pictureView : WBPictrueView = WBPictrueView()
    
}
